/*
 * JFrmProduto.java
 *
 * Created on 10 de Agosto de 2008, 21:48
 */

package view;

import controller.CtrCadastroFornecedor;
import controller.CtrCadastroProduto;
import java.util.List;
import javax.swing.JOptionPane;
import model.Fornecedor;
import model.Produto;
import utils.ExcecaoSCEV;




/**
 *
 * @author  Administrador
 */
public class JFrmProduto extends javax.swing.JDialog {
     int operacao;
    private boolean sucesso = false;
    CtrCadastroProduto ctrCadastroProduto = CtrCadastroProduto.getInstance() ;
    Produto produto;

    CtrCadastroFornecedor ctrCadastroFornecedor = CtrCadastroFornecedor.getInstance() ;
    Fornecedor fornecedor;
    List<Fornecedor> fornecedores;
    
    
    
    /** Creates new form PropProdutoJDialog */
    public JFrmProduto(javax.swing.JFrame parent,int operacao, Produto umProduto) {
        
        super(parent,true);
        initComponents();
        this.operacao = operacao;
        produto = umProduto;


          try {
            fornecedores = ctrCadastroFornecedor.listarTodos(1);
            carregarFornecedor();
        } catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
        }


        switch (operacao) {
            case 0:
                this.setTitle("Inclusão de produto");
                jTextFieldCodigo.setEditable(true);
                break;
            case 1:
                this.setTitle("Alteração de produto");
                setDados();
                break;

            case 2:
                this.setTitle("Consulta de produto");
                jTextFieldDescricao.setEditable(false);
                jTextFieldAlbum.setEditable(false);            
                jTextFieldQuantidade.setEditable(false);
                jTextFieldGenero.setEditable(false);
                jTextFieldPreco.setEditable(false);
                jTextFieldPrecoCusto.setEditable(false);
                jComboBoxTipo.setEnabled(false);
                jTextFieldCodigo.setText(produto.getCodigoProduto().toString());
                jButtonCancelar.setEnabled(false);
                jComboBoxFornecedor.setEnabled(false);
                setDados();
                break;


        }


        this.getRootPane().setDefaultButton(jButtonOK);

    }

 private void carregarFornecedor() {

        int posicao = 0;
        jComboBoxFornecedor.removeAllItems();
        int i = 0;
        if (!fornecedores.isEmpty()) {
            for (Fornecedor umFornecedor : fornecedores) {
                jComboBoxFornecedor.addItem(umFornecedor.getCodigoFornecedor() + "-" + umFornecedor.getNome());
                if (operacao != 0)
                {
                    if (produto.getFornecedor().getCodigoFornecedor().intValue()==umFornecedor.getCodigoFornecedor().intValue())
                    {
                        posicao = i;
                    }
                }
                i++;
            }
            jComboBoxFornecedor.setSelectedIndex(posicao);
        }
 }
   
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelSuperior = new javax.swing.JPanel();
        jLabelCodigo = new javax.swing.JLabel();
        jLabelAlbum = new javax.swing.JLabel();
        jLabelDescricao = new javax.swing.JLabel();
        jLabelQuantidade = new javax.swing.JLabel();
        jTextFieldCodigo = new javax.swing.JTextField();
        jTextFieldAlbum = new javax.swing.JTextField();
        jTextFieldDescricao = new javax.swing.JTextField();
        jTextFieldQuantidade = new javax.swing.JTextField();
        jLabelGenero = new javax.swing.JLabel();
        jTextFieldGenero = new javax.swing.JTextField();
        jLabelTipo = new javax.swing.JLabel();
        jComboBoxTipo = new javax.swing.JComboBox();
        jLabelPreco = new javax.swing.JLabel();
        jTextFieldPrecoCusto = new javax.swing.JTextField();
        jLabelFornecedor = new javax.swing.JLabel();
        jComboBoxFornecedor = new javax.swing.JComboBox();
        jLabelPrecoCusto = new javax.swing.JLabel();
        jTextFieldPreco = new javax.swing.JTextField();
        jPanelInferior = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelCodigo.setText("Código");

        jLabelAlbum.setText("Album");

        jLabelDescricao.setText("Descrição");

        jLabelQuantidade.setText("Quantidade");

        jTextFieldCodigo.setEditable(false);

        jTextFieldDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldDescricaoKeyTyped(evt);
            }
        });

        jTextFieldQuantidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldQuantidadeActionPerformed(evt);
            }
        });

        jLabelGenero.setText("Genero");

        jLabelTipo.setText("Tipo");

        jComboBoxTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CD", "CD Duplo", "DVD", "DVD Duplo", "LP", "LP Duplo" }));

        jLabelPreco.setText("Preço");

        jLabelFornecedor.setText("Fornecedor");

        jComboBoxFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxFornecedorActionPerformed(evt);
            }
        });

        jLabelPrecoCusto.setText("Preço de custo");

        org.jdesktop.layout.GroupLayout jPanelSuperiorLayout = new org.jdesktop.layout.GroupLayout(jPanelSuperior);
        jPanelSuperior.setLayout(jPanelSuperiorLayout);
        jPanelSuperiorLayout.setHorizontalGroup(
            jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelSuperiorLayout.createSequentialGroup()
                .add(31, 31, 31)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabelPreco)
                    .add(jLabelPrecoCusto)
                    .add(jPanelSuperiorLayout.createSequentialGroup()
                        .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabelDescricao)
                            .add(jLabelAlbum)
                            .add(jLabelCodigo)
                            .add(jLabelQuantidade)
                            .add(jLabelGenero)
                            .add(jLabelTipo)
                            .add(jLabelFornecedor))
                        .add(25, 25, 25)
                        .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jTextFieldQuantidade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jTextFieldCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jTextFieldAlbum)
                                .add(jTextFieldDescricao, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                .add(jTextFieldGenero))
                            .add(jComboBoxTipo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jTextFieldPrecoCusto)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jComboBoxFornecedor, 0, 119, Short.MAX_VALUE)))))
                .addContainerGap(173, Short.MAX_VALUE))
            .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanelSuperiorLayout.createSequentialGroup()
                    .add(114, 114, 114)
                    .add(jTextFieldPreco, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 111, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(222, Short.MAX_VALUE)))
        );
        jPanelSuperiorLayout.setVerticalGroup(
            jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelSuperiorLayout.createSequentialGroup()
                .add(36, 36, 36)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelCodigo)
                    .add(jTextFieldCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelDescricao)
                    .add(jTextFieldDescricao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelAlbum)
                    .add(jTextFieldAlbum, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelQuantidade)
                    .add(jTextFieldQuantidade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelGenero)
                    .add(jTextFieldGenero, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelTipo)
                    .add(jComboBoxTipo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(7, 7, 7)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelPrecoCusto)
                    .add(jTextFieldPrecoCusto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabelPreco)
                .add(13, 13, 13)
                .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelFornecedor)
                    .add(jComboBoxFornecedor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
            .add(jPanelSuperiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanelSuperiorLayout.createSequentialGroup()
                    .add(259, 259, 259)
                    .add(jTextFieldPreco, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(86, Short.MAX_VALUE)))
        );

        jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/incluirPequeno.png"))); // NOI18N
        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/excluirPequeno.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jButtonCancelar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jButtonCancelarFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jButtonCancelarFocusLost(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelInferiorLayout = new org.jdesktop.layout.GroupLayout(jPanelInferior);
        jPanelInferior.setLayout(jPanelInferiorLayout);
        jPanelInferiorLayout.setHorizontalGroup(
            jPanelInferiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelInferiorLayout.createSequentialGroup()
                .add(105, 105, 105)
                .add(jButtonOK)
                .add(72, 72, 72)
                .add(jButtonCancelar)
                .addContainerGap(95, Short.MAX_VALUE))
        );
        jPanelInferiorLayout.setVerticalGroup(
            jPanelInferiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelInferiorLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelInferiorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonOK)
                    .add(jButtonCancelar))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(jPanelInferior, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(jPanelSuperior, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jPanelSuperior, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelInferior, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        try {
            // TODO add your handling code here:
            
            produto.setCodigoProduto(Integer.parseInt(jTextFieldCodigo.getText()));
            produto.setDescricao(jTextFieldDescricao.getText());
            produto.setAlbum(jTextFieldAlbum.getText());
            produto.setGenero (jTextFieldGenero.getText());
            produto.setTipo(jComboBoxTipo.getSelectedItem().toString());
            produto.setPreco (jTextFieldPreco.getText());
            produto.setPrecoCusto (jTextFieldPrecoCusto.getText());
            produto.setQuantidade(Integer.parseInt(jTextFieldQuantidade.getText()));
            produto.setFornecedor(fornecedores.get(jComboBoxFornecedor.getSelectedIndex()));


            switch (operacao) {
                case 0:
   
                    ctrCadastroProduto.incluir(produto);
                    break;
                case 1 :                    
                    ctrCadastroProduto.alterar(produto);
                    break;
               
            }
            sucesso = true;
            setVisible(false);
        } 
        catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null,ex.getMsg(),"Validação",JOptionPane.ERROR_MESSAGE);
            jTextFieldDescricao.requestFocus();
        }
        catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null,"Valor inválido.","Validação",JOptionPane.ERROR_MESSAGE);
            jTextFieldCodigo.requestFocus();
        }
}//GEN-LAST:event_jButtonOKActionPerformed

public void setDados(){
    jTextFieldCodigo.setText(produto.getCodigoProduto().toString());
    jTextFieldDescricao.setText(produto.getDescricao());
    jTextFieldAlbum.setText(produto.getAlbum());
    jTextFieldGenero.setText(produto.getGenero());
    jComboBoxTipo.setSelectedItem(produto.getTipo());
    jTextFieldPreco.setText(produto.getPreco());
    jTextFieldPrecoCusto.setText(produto.getPrecoCusto());
    jTextFieldQuantidade.setText(produto.getQuantidade().toString());
    jComboBoxFornecedor.setSelectedItem(produto.getFornecedor());
}




private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
         setVisible(false);
}//GEN-LAST:event_jButtonCancelarActionPerformed

private void jButtonCancelarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jButtonCancelarFocusGained
      this.getRootPane().setDefaultButton(jButtonCancelar);
}//GEN-LAST:event_jButtonCancelarFocusGained

private void jButtonCancelarFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jButtonCancelarFocusLost
      this.getRootPane().setDefaultButton(jButtonOK);
}//GEN-LAST:event_jButtonCancelarFocusLost

private void jTextFieldDescricaoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldDescricaoKeyTyped
   
}//GEN-LAST:event_jTextFieldDescricaoKeyTyped

private void jTextFieldQuantidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldQuantidadeActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldQuantidadeActionPerformed

private void jComboBoxFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxFornecedorActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBoxFornecedorActionPerformed



    /**
    * @param args the command line arguments
    */
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JComboBox jComboBoxFornecedor;
    private javax.swing.JComboBox jComboBoxTipo;
    private javax.swing.JLabel jLabelAlbum;
    private javax.swing.JLabel jLabelCodigo;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelFornecedor;
    private javax.swing.JLabel jLabelGenero;
    private javax.swing.JLabel jLabelPreco;
    private javax.swing.JLabel jLabelPrecoCusto;
    private javax.swing.JLabel jLabelQuantidade;
    private javax.swing.JLabel jLabelTipo;
    private javax.swing.JPanel jPanelInferior;
    private javax.swing.JPanel jPanelSuperior;
    private javax.swing.JTextField jTextFieldAlbum;
    private javax.swing.JTextField jTextFieldCodigo;
    private javax.swing.JTextField jTextFieldDescricao;
    private javax.swing.JTextField jTextFieldGenero;
    private javax.swing.JTextField jTextFieldPreco;
    private javax.swing.JTextField jTextFieldPrecoCusto;
    private javax.swing.JTextField jTextFieldQuantidade;
    // End of variables declaration//GEN-END:variables

 public boolean isSucesso() {
        return sucesso;
    }
}
